package ai.people.platform.archives.controller;

import ai.people.netmon.framework.domain.archives.request.GangGroupListRequest;
import ai.people.netmon.framework.domain.archives.request.GangGroupSaveOrUpdateRequest;
import ai.people.netmon.framework.domain.archives.valid.GangGroupVoValidationGroup;
import ai.people.netmon.framework.domain.archives.vo.*;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.platform.archives.service.GangGroupInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 团伙库接口
 *
 * @author zhenglin
 * @date 2022/6/21
 */
@RestController
@RequestMapping("/gang")
public class GangGroupController {

    @Autowired
    GangGroupInfoService gangGroupInfoService;


    /**
     * 团伙列表
     *
     * @param request 请求
     * @return {@link Result}<{@link PageResponse}<{@link GangGroupListVO}>>
     */
    @PreAuthorize("hasAuthority('gang:group')")
    @PostMapping("/list")
    public Result<PageResponse<GangGroupListVO>> listGangGroup(@Validated @RequestBody GangGroupListRequest request) {
        PageResponse<GangGroupListVO> response = gangGroupInfoService.listGangGroup(request);
        return ResultUtil.success(response);
    }


    /**
     * 保存团伙(基本详情+组织关系)
     *
     * @param group 保存团伙详情
     * @return {@link Result}<{@link Boolean}>
     * @throws Exception 异常
     */
    @PreAuthorize("hasAuthority('gang:group')")
    @PostMapping
    public Result<Boolean> save(@Validated @RequestBody GangGroupSaveOrUpdateRequest group) throws Exception {
        gangGroupInfoService.saveGangGroupGraph(group);
        return ResultUtil.success();
    }

    /**
     * 更新团伙(基本详情+组织关系)
     *
     * @param group 更新团伙信息
     * @return {@link Result}<{@link Boolean}>
     * @throws Exception 异常
     */
    @PreAuthorize("hasAuthority('gang:group')")
    @PutMapping
    public Result<Boolean> update(@Validated(GangGroupVoValidationGroup.Update.class) @RequestBody GangGroupSaveOrUpdateRequest group) throws Exception {
        gangGroupInfoService.updateGangGroupGraph(group);
        return ResultUtil.success();
    }

    /**
     * 团伙详情
     *
     * @param id 团伙ID
     * @return {@link Result}<{@link GangGroupDetailVO}>
     * @throws Exception 异常
     */
    @GetMapping("/detail/{id}")
    public Result<GangGroupDetailVO> getGangGroupGraph(@PathVariable("id") Long id) throws Exception {
        GangGroupDetailVO info = gangGroupInfoService.getGangGroupGraph(id);
        return ResultUtil.success(info);
    }

    /**
     * 团伙成员列表
     *
     * @param id 团伙ID
     * @return {@link Result}<{@link List}<{@link GangGroupOrgPersonListVO}>>
     * @throws Exception 异常
     */
    @GetMapping("/org/person/list/{id}")
    public Result<List<GangGroupOrgPersonListVO>> listGangGroupPersonGraph(@PathVariable("id") Long id) throws Exception {
        List<GangGroupOrgPersonListVO> response = gangGroupInfoService.listGangGroupPersonGraph(id);
        return ResultUtil.success(response);
    }

}
